package com.pi.tools;



import java.util.Calendar;

import java.util.Date;


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;



public class GPSUtilities {

	/**

	 * 把字节指定的位数设为1，不管原来该位是什么。

	 * 

	 * @param data

	 * @param pos

	 *            从0开始

	 */

	public static byte setByteBitOne(byte data, int pos) {

		byte compare = (byte) Math.pow(2.0, pos);

		data = (byte) (data | compare);

		return data;

	}



	/**

	 * 把字节指定的位数设为0，不管原来该位是什么。

	 * 

	 * @param data

	 * @param pos

	 */

	public static byte setByteBitZero(byte data, int pos) {

		byte compare = (byte) ~(byte) Math.pow(2.0, pos);

		data = (byte) (data & compare);

		return data;

	}



	/**

	 * 返回指定字节第几个位的数字，只可能是０或者１

	 * 

	 * @param data

	 * @param pos

	 *            从0开始的位数

	 * @return

	 */

	public static byte getByteBit(byte data, int pos) {

		byte bitData = 0;



		byte compare = (byte) Math.pow(2.0, pos);

		if ((data & compare) == compare) {

			bitData = 1;



		}



		return bitData;



	}



	/**

	 * 返回一个字节的每一位的数字

	 * 

	 * @param data

	 * @return

	 */

	public static byte[] getByteBits(byte data) {

		byte[] results = new byte[8];

		for (int i = 0; i < 8; i++) {

			results[i] = (byte) GPSUtilities.getByteBit(data, i);



		}

		return results;

	}



	/**

	 * 长整型装换成字节数组

	 * 

	 * @param x

	 * @return

	 */

	public static byte[] longToByteArray(long x) {

		byte[] bb = new byte[8];

		bb[0] = (byte) (x >> 56);

		bb[1] = (byte) (x >> 48);

		bb[2] = (byte) (x >> 40);

		bb[3] = (byte) (x >> 32);

		bb[4] = (byte) (x >> 24);

		bb[5] = (byte) (x >> 16);

		bb[6] = (byte) (x >> 8);

		bb[7] = (byte) (x >> 0);

		return bb;

	}



	/**

	 * 短整型装换成字节数组

	 * 

	 * @param x

	 * @return

	 */

	public static byte[] shortToByteArray(short x) {

		byte[] bb = new byte[2];

		bb[0] = (byte) (x >> 8);

		bb[1] = (byte) (x >> 0);

		return bb;

	}



	/**

	 * 整型装换成字节数组

	 * 

	 * @param x

	 * @return

	 */

	public static byte[] intToByteArray(int x) {

		byte[] bb = new byte[4];

		bb[0] = (byte) (x >> 24);

		bb[1] = (byte) (x >> 16);

		bb[2] = (byte) (x >> 8);

		bb[3] = (byte) (x >> 0);

		return bb;

	}



	/**

	 * Base64编码

	 * 

	 * @param text

	 * @return

	 */

	public static byte[] base64Encode(byte[] buf) {

		return Base64.encode(buf).getBytes();

	}



	public static byte[] base64Encode(byte[] buf, int start, int length) {

		byte[] t = new byte[length];

		System.arraycopy(buf, start, t, 0, length);

		byte[] base64Encode = Base64.encode(t).getBytes();

		byte[] finalmessage = new byte[base64Encode.length + 2];

		finalmessage[0] = 0x7E;

		System.arraycopy(base64Encode, 0, finalmessage, 1, base64Encode.length);

		finalmessage[base64Encode.length + 1] = 0x7F;

		buf = null;

		t = null;

		base64Encode = null;

		return finalmessage;

	}



	/**

	 * Base64解码

	 */

	public static byte[] base64Decode(String text) {

		return Base64.decode(text);

	}



	/**

	 * 计算校验码

	 */

	public static short getCRCCode(byte[] code) {

		return CRC.do_crc((char) 0, code, code.length);

	}



	public static short getCRCCode(byte[] code, int start, int end) {

		return CRC.do_crc((char) 0, code, start, end);

	}



	/**

	 * 字节数组转换成short

	 * 

	 */

	public static short getShort(byte[] b) {

		return (short) (((b[0] << 8) | b[1] & 0xff));

	}



	/**

	 * 根据字节数组获得短整形数据

	 * 

	 * @param b

	 * @param index

	 * @return

	 */

	public static short getShort(byte[] b, int index) {

		return (short) (((b[index] << 8) | b[index + 1] & 0xff));

	}



	/**

	 * 根据下标和字节数组获得整型数据

	 * 

	 * @param bb

	 * @param index

	 * @return

	 */

	public static int getInt(byte[] bb, int index) {

		return (int) ((((bb[index + 0] & 0xff) << 24)

				| ((bb[index + 1] & 0xff) << 16)

				| ((bb[index + 2] & 0xff) << 8) | ((bb[index + 3] & 0xff) << 0)));

	}



	public static int getInt(byte[] bb) {

		return (int) ((((bb[0] & 0xff) << 24) | ((bb[1] & 0xff) << 16)

				| ((bb[2] & 0xff) << 8) | ((bb[3] & 0xff) << 0)));

	}



	// 获得无符号整形

	public static long getUnsignedInt(byte[] bb) {

		return ((int) ((((bb[0] & 0xff) << 24) | ((bb[1] & 0xff) << 16)

				| ((bb[2] & 0xff) << 8) | ((bb[3] & 0xff) << 0)))) & 0xffffffffL;

	}



	// 获得无符号短整形

	public static int getUnsignedShort(byte[] bb) {

		return ((short) (((bb[0] << 8) | bb[1] & 0xff))) & 0xffff;

	}



	// 获得无符号字节

	public static short getUnsignedByte(byte bb) {

		return (short) (bb & 0xff);

	}



	/**

	 * 根据下标和字节数组获得长整型数据

	 * 

	 * @param bb

	 * @param index

	 * @return

	 */

	public static long getLong(byte[] bb, int index, boolean isint) {

		if (!isint) {

			return ((((long) bb[index + 0] & 0xff) << 56)

					| (((long) bb[index + 1] & 0xff) << 48)

					| (((long) bb[index + 2] & 0xff) << 40)

					| (((long) bb[index + 3] & 0xff) << 32)

					| (((long) bb[index + 4] & 0xff) << 24)

					| (((long) bb[index + 5] & 0xff) << 16)

					| (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));

		} else {

			return ((((long) bb[index + 0] & 0xff) << 24)

					| (((long) bb[index + 1] & 0xff) << 16)

					| (((long) bb[index + 2] & 0xff) << 8) | (((long) bb[index + 3] & 0xff) << 0));

		}

	}



	// 字节数组转化为16进制字符串

	public static String bytesToHexString(byte[] src) {

		StringBuilder stringBuilder = new StringBuilder("");

		if (src == null || src.length <= 0) {

			return "";

		}

		for (int i = 0; i < src.length; i++) {

			int v = src[i] & 0xFF;

			String hv = Integer.toHexString(v);

			if (hv.length() < 2) {

				stringBuilder.append(0);

			}

			stringBuilder.append(hv);

		}

		return stringBuilder.toString();

	}



	public static String bytesToHexString2(byte[][] src) {

		StringBuilder stringBuilder = new StringBuilder("");

		for (int i = 0; i < src.length; i++) {

			stringBuilder.append(bytesToHexString(src[i]));

		}

		return stringBuilder.toString();

	}



	/**

	 * 数据转义 在帧头7EH和帧尾7FH之间的数据出现的7DH、7EH、7FH分别与20H异或，

	 * 变成（5DH、5EH、5FH）再在前加7DH，如收到一个字节是7DH，经过转义后变为7DH，5DH

	 */

	public static byte[] dataEscape(byte[] data, int start, int end) {

		int count = 0;

		int datal = end - start + 1;

		for (int i = start; i <= end; i++) {

			if (data[i] == 0x7D) {

				count++;

			}

		}

		if (count == 0) {

			return data;

		} else {

			int j = 0;

			byte[] deEscape = new byte[datal + count];

			for (int i = start; i <= end; i++) {

				if (data[i] == 0x7D) {

					if (data[i] == 0x7D) {

						deEscape[j] = 0x7D;

						deEscape[j + 1] = 0x5D;

					} else if (data[i] == 0x7E) {

						deEscape[j] = 0x7D;

						deEscape[j + 1] = 0x7E;

					} else if (data[i] == 0x7F) {

						deEscape[j] = 0x7D;

						deEscape[j + 1] = 0x5F;

					}

					j++;

				} else {

					deEscape[j] = data[i];

				}

			}

			return deEscape;

		}



	}



	/**

	 * 数据反转义

	 */

	public static byte[] dataDeEscape(byte[] data) {

		int count = 0;

		int datal = data.length;

		for (int i = 0; i < datal; i++) {

			if (data[i] == 0x7D) {

				count++;

			}

		}

		if (count == 0) {

			return data;

		} else {

			int j = 0;

			byte[] deEscape = new byte[datal - count];

			for (int i = 0; i < datal; i++) {

				if (data[i] == 0x7D) {

					if (data[i + 1] == 0x5D) {

						deEscape[j] = 0x7D;

					} else if (data[i + 1] == 0x5E) {

						deEscape[j] = 0x7E;

					} else if (data[i + 1] == 0x5F) {

						deEscape[j] = 0x7F;

					}

					i++;

				} else {

					deEscape[j] = data[i];

				}

				j++;

			}

			return deEscape;

		}



	}



	public static byte[] encrypt(long key, byte[] data, int size) {

		if (key == 0) {

			key = 1;

		}



		for (int i = 0; i < size; i++) {

			key = GPSConstant.IA1 * (key % GPSConstant.M1) + GPSConstant.IC1;

			// System.out.println("key" + i + ":" + key);

			data[i] = (byte) (data[i] ^ ((key >> 20) & 0xFF));

		}

		return data;

	}



	// 协议加密start加密开始下标，length加密长度

	public static void encrypt(long key, byte[] data, int start, int length) {

		int end = start + length;

		if (key == 0) {

			key = 1;

		}



		for (int i = start; i < end; i++) {

			key = GPSConstant.IA1 * (key % GPSConstant.M1) + GPSConstant.IC1;

			// System.out.println("key" + i + ":" + key);

			data[i] = (byte) (data[i] ^ ((key >> 20) & 0xFF));

		}

	}



	public static byte byteEncrypt(int key, byte data) {

		if (key == 0) {

			key = 1;

		}

		key = GPSConstant.IA1 * (key % GPSConstant.M1) + GPSConstant.IC1;

		// System.out.println("key:" + key);

		data = (byte) (byte) (data ^ ((key >> 20) & 0xFF));

		return data;

	}



	// 根据原始数据包获取指定坐标之间的数据

	public static byte[] getPactnumAndPact(byte[] data, int start, int length) {

		byte[] t = new byte[length];

		System.arraycopy(data, start, t, 0, length);

		return t;

	}



	public static void main(String[] args) {

		// 加密解密测试

		// String text = "gps";

		//

		// byte[] a = encrypt(999999999, text.getBytes(),

		// (short) text.getBytes().length);

		// String b = new String(a);

		// System.out.println(b);

		//

		// byte[] c = encrypt(999999999, b.getBytes(), b.getBytes().length);

		// String d = new String(c);

		// System.out.println(d);

		//

		// byte[] e = { byteEncrypt(999999999, "p".getBytes()[0]) };

		// String f = new String(e);

		// System.out.println(f);



		// crc校验测试

		// byte[] bytes = { 0x47, 0x01, 0x00, 0x0C, 0x7A, 0x18, 0x2C, (byte)

		// 0x92,

		// 0x00, 0x00, 0x16, 0x2E, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x02,

		// 0x44, 0x01 };

		// System.out.println(getCRCCode(bytes));



		// // base64测试

		// byte[] a = { 0x47, 0x01, 0x00, 0x0C, 0x7A, 0x18, 0x2C, (byte) 0x92,

		// 0x00, 0x00, 0x16, 0x2E, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x02,

		// 0x44, 0x01, 0x76, 0x18 };

		//

		// byte[] b = { 0x52, 0x77, 0x45, 0x41, 0x44, 0x48, 0x6F, 0x59, 0x4C,

		// 0x4A, 0x49, 0x41, 0x41, 0x42, 0x59, 0x75, 0x41, 0x41, 0x41,

		// 0x41, 0x43, 0x67, 0x41, 0x43, 0x52, 0x41, 0x46, 0x32, 0x47,

		// 0x41, 0x3D, 0x3D };

		//

		// byte[] aa = { 0x52, 0x77, 0x45, 0x41, 0x44, 0x48, 0x6d, 0x66, 0x6d,

		// 0x38, 0x30, 0x41, 0x41, 0x42, 0x59, 0x75, 0x41, 0x41, 0x41,

		// 0x41, 0x43, 0x67, 0x41, 0x67, 0x56, 0x51, 0x41, 0x51, 0x41,

		// 0x77, 0x67, 0x55, 0x4a, 0x44, 0x4d, 0x52, 0x51, 0x6c, 0x51,

		// 0x33, 0x41, 0x6a, 0x43, 0x47, 0x43, 0x51, 0x41, 0x6f, 0x41,

		// 0x42, 0x59, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x79, 0x41,

		// 0x42, 0x42, 0x41, 0x41, 0x41, 0x41, 0x41, 0x43, 0x6e, 0x77,

		// 0x51, 0x3d, 0x3d };

		// byte[] c = base64Encode(a);

		//

		// for (int i = 0; i < c.length; i++) {

		// byte c2 = c[i];

		// System.out.print(Integer.toHexString(c2) + " ");

		// }

		// System.out.println();

		// byte[] d = base64Decode(new String(aa));

		// for (int i = 0; i < d.length; i++) {

		// byte d2 = d[i];

		// System.out.print(Integer.toHexString(d2) + " ");

		// }

		//

		// String ccc = byteArrayToString(b);

		//

		// System.out.println("ccc===" + ccc);

		//

		// byte[] ddd = stringTobyteArray(ccc);

		//

		// byte[] cccc = { 0x10, 3, 7, 22, 0, 0 };



		// byte[] ddd = { 0x47, 0x01, 0x00, 0x0C, 0x79, (byte) 0x9F, (byte)

		// 0x9B,

		// (byte) 0xCD, 0x00, 0x00, 0x16, 0x2E, 0x00, 0x00, 0x00, 0x0A,

		// 0x00, 0x20, 0x55, 0x01, 0x10, 0x03, 0x08, 0x14, 0x43, 0x54,

		// 0x11, 0x42, 0x54, 0x32, 0x02, 0x30, (byte) 0x86, 0x06, 0x00,

		// 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x03, 0x20, 0x01,

		// 0x04, 0x00, 0x00, 0x00, 0x00 };

		//

		// System.out.println(getCRCCode(ddd));

		System.out.println(toHex("12"));

		byte[] b = { 30, 97 };

		System.out.println(getShort(b) & 0xFF);

	}



	public static byte[] minaBase64Decode(String encoded) {

		return Base64.decode(encoded);

	}



	public int toUnsigned(int s) {

		return s & 0x0FFFF;

	}



	public static Date BCDToDate(byte[] bcd, String dateType) {

		Calendar calendar = Calendar.getInstance();

		if ("nyrsfm".equals(dateType)) {

			int year = Integer.parseInt(Integer.toHexString(bcd[0]));

			if (year < 60) {

				year = Integer.parseInt("20" + Integer.toHexString(bcd[0]));

			} else {

				year = Integer.parseInt("19" + Integer.toHexString(bcd[0]));

			}



			calendar.set(Calendar.YEAR, year);

			calendar.set(Calendar.MONTH,

					Integer.parseInt(Integer.toHexString(bcd[1])) - 1);

			calendar.set(Calendar.DATE,

					Integer.parseInt(Integer.toHexString(bcd[2])));

			calendar.set(Calendar.HOUR_OF_DAY,

					Integer.parseInt(Integer.toHexString(bcd[3])));

			calendar.set(Calendar.MINUTE,

					Integer.parseInt(Integer.toHexString(bcd[4])));

			calendar.set(Calendar.SECOND,

					Integer.parseInt(Integer.toHexString(bcd[5])));

		} else if ("nyr".equals(dateType)) {

			int year = Integer.parseInt(Integer.toHexString(bcd[0]));

			if (year < 60) {

				year = Integer.parseInt("20" + Integer.toHexString(bcd[0]));

			} else {

				year = Integer.parseInt("19" + Integer.toHexString(bcd[0]));

			}



			calendar.set(Calendar.YEAR, year);

			calendar.set(Calendar.MONTH,

					Integer.parseInt(Integer.toHexString(bcd[1])) - 1);

		}

		return calendar.getTime();

	}



	public static String BCDToDate(byte[] bcd) {

		int year = Integer.parseInt(Integer.toHexString(bcd[0]));

		if (year < 60) {

			year = Integer.parseInt("20" + Integer.toHexString(bcd[0]));

		} else {

			year = Integer.parseInt("19" + Integer.toHexString(bcd[0]));

		}

		StringBuffer databuff = new StringBuffer();

		databuff.append(year + "");

		databuff.append(Integer.parseInt(Integer.toHexString(bcd[1])) >= 10 ? Integer

				.parseInt(Integer.toHexString(bcd[1])) + ""

				: "0" + (Integer.parseInt(Integer.toHexString(bcd[1]))));

		databuff.append(Integer.parseInt(Integer.toHexString(bcd[2])) >= 10 ? Integer

				.parseInt(Integer.toHexString(bcd[2])) + ""

				: "0" + (Integer.parseInt(Integer.toHexString(bcd[2]))));

		databuff.append(Integer.parseInt(Integer.toHexString(bcd[3])) >= 10 ? Integer

				.parseInt(Integer.toHexString(bcd[3])) + ""

				: "0" + (Integer.parseInt(Integer.toHexString(bcd[3]))));

		databuff.append(Integer.parseInt(Integer.toHexString(bcd[4])) >= 10 ? Integer

				.parseInt(Integer.toHexString(bcd[4])) + ""

				: "0" + (Integer.parseInt(Integer.toHexString(bcd[4]))));

		databuff.append(Integer.parseInt(Integer.toHexString(bcd[5])) >= 10 ? Integer

				.parseInt(Integer.toHexString(bcd[5])) + ""

				: "0" + (Integer.parseInt(Integer.toHexString(bcd[5]))));

		return databuff.toString();

	}



	public static String byteArrayToString(byte[] ba) {

		String con = "";

		for (int i = 0; i < ba.length; i++) {

			con += Integer.toHexString(ba[i]) + " ";

		}

		return con;

	}



	public static byte[] stringTobyteArray(String s) {

		String[] t = s.split(" ");

		byte[] f = new byte[t.length];



		for (int i = 0; i < f.length; i++) {

			f[i] = (byte) (Byte.decode("0x" + t[i]));

		}

		return f;

	}



	/**

	 * 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,

	 * 0xD9}

	 * 

	 * @param src

	 *            String

	 * @return byte[]

	 */

	public static byte[] HexString2Bytes(String src) {

		byte[] ret = new byte[8];

		byte[] tmp = src.getBytes();

		for (int i = 0; i < 8; i++) {

			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);

		}

		return ret;

	}



	/**

	 * 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF

	 * 

	 * @param src0

	 *            byte

	 * @param src1

	 *            byte

	 * @return byte

	 */

	public static byte uniteBytes(byte src0, byte src1) {

		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))

				.byteValue();

		_b0 = (byte) (_b0 << 4);

		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))

				.byteValue();

		byte ret = (byte) (_b0 ^ _b1);

		return ret;

	}



	/**

	 * 将十进制字符串转化成十六进制

	 * 

	 * @param data

	 * @return

	 */

	public static int toHex(String data) {

		return Integer.valueOf(data, 16);

	}



	/**

	 * 

	 * @param cal

	 * @param timeType

	 * @return

	 */

	public static byte[] dateToBCD(Calendar cal, String timeType) {

		byte[] content = null;

		if ("nyrsfm".equals(timeType)) {

			// 年月日时分秒

			content = new byte[6];

			String year = (cal.get(Calendar.YEAR) + "").substring(2, 4);

			content[0] = (byte) toHex(year);

			content[1] = (byte) toHex(cal.get(Calendar.MONTH) + 1 + "");

			content[2] = (byte) toHex(cal.get(Calendar.DATE) + "");

			content[3] = (byte) toHex(cal.get(Calendar.HOUR_OF_DAY) + "");

			content[4] = (byte) toHex(cal.get(Calendar.MINUTE) + "");

			content[5] = (byte) toHex(cal.get(Calendar.SECOND) + "");

		} else if ("nyr".equals(timeType)) {

			// 年月日

			content = new byte[3];

			String year = (cal.get(Calendar.YEAR) + "").substring(2, 4);

			content[0] = (byte) toHex(year);

			content[1] = (byte) toHex(cal.get(Calendar.MONTH) + 1 + "");

			content[2] = (byte) toHex(cal.get(Calendar.DATE) + "");

		} else if ("yrsf".equals(timeType)) {

			// 月日时分

			content = new byte[4];

			content[0] = (byte) toHex(cal.get(Calendar.MONTH) + 1 + "");

			content[1] = (byte) toHex(cal.get(Calendar.DATE) + "");

			content[2] = (byte) toHex(cal.get(Calendar.HOUR_OF_DAY) + "");

			content[3] = (byte) toHex(cal.get(Calendar.MINUTE) + "");

		} else if ("rsf".equals(timeType)) {

			// 日时分

			content = new byte[3];

			content[0] = (byte) toHex(cal.get(Calendar.DATE) + "");

			content[1] = (byte) toHex(cal.get(Calendar.HOUR_OF_DAY) + "");

			content[2] = (byte) toHex(cal.get(Calendar.MINUTE) + "");

		} else if ("nyrsfm_7".equals(timeType)) {

			// 年月日时分秒

			content = new byte[7];

			String year_1 = (cal.get(Calendar.YEAR) + "").substring(0, 2);

			String year_2 = (cal.get(Calendar.YEAR) + "").substring(2, 4);

			content[0] = (byte) toHex(year_1);

			content[1] = (byte) toHex(year_2);

			content[2] = (byte) toHex(cal.get(Calendar.MONTH) + 1 + "");

			content[3] = (byte) toHex(cal.get(Calendar.DATE) + "");

			content[4] = (byte) toHex(cal.get(Calendar.HOUR_OF_DAY) + "");

			content[5] = (byte) toHex(cal.get(Calendar.MINUTE) + "");

			content[6] = (byte) toHex(cal.get(Calendar.SECOND) + "");

		}

		return content;

	}



	// 获得报警状态

	public static byte getMsgStatus(byte[] pactContent) {

		long status = GPSUtilities.getLong(pactContent, 22, false);

		GpsCarstat carstat = new GpsCarstat(status);

		byte alarmtype = 0;

		if (carstat.getPreyAlarm() == 1) {

			// 紧急报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 7);

		}

		if (carstat.getPreyAlarm() == 0) {

			// 紧急报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 7);

		}

		if (carstat.getSpeedAlarm() == 1) {

			// 超速报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 6);

		}

		if (carstat.getSpeedAlarm() == 0) {

			// 超速报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 6);

		}

		if (carstat.getShakeAlarm() == 1) {

			// 震动报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 5);

		}

		if (carstat.getShakeAlarm() == 0) {

			// 震动报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 5);

		}

		if (carstat.getMainPower() == 1) {

			// 主电低报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 4);

		}

		if (carstat.getMainPower() == 0) {

			// 主电低报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 4);

		}

		if (carstat.getOutstepAlarm() == 1) {

			// 出界报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 3);

		}

		if (carstat.getOutstepAlarm() == 0) {

			// 出界报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 3);

		}

		if (carstat.getInsetpAlarm() == 1) {

			// 入界报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 2);

		}

		if (carstat.getInsetpAlarm() == 0) {

			// 入界报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 2);

		}

		if (carstat.getFDrivingALarm() == 1) {

			// 疲劳驾驶报警

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 1);

		}

		if (carstat.getFDrivingALarm() == 0) {

			// 疲劳驾驶报警

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 1);

		}

		if (carstat.getOverstepAlarm() == 1) {

			// 地理栅栏（道路监控报警）

			alarmtype = GPSUtilities.setByteBitOne(alarmtype, 0);

		}

		if (carstat.getOverstepAlarm() == 0) {

			// 地理栅栏（道路监控报警）

			alarmtype = GPSUtilities.setByteBitZero(alarmtype, 0);

		}

		return alarmtype;

	}

}

